package com.xindaoapp.happypet.viewpager.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.bean.Message.Pic;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.TimeFormat;
import com.xindaoapp.happypet.view.CircleImageView;

/**
 * 我的帖子发表
 * 
 * @author chenghao
 * 
 */
public class MyPostAdapter extends XinDaoBaseAdapter<Message> {
    private int photoWidth = 0;
    private final int tabIndex;
    private boolean isClickEnable;

    public MyPostAdapter(Context mContext, List<Message> iniData, int pageSize, int res,
            int loadingRes, int tabIndex) {
        super(mContext, iniData, pageSize, res, loadingRes);
        this.tabIndex = tabIndex;
        photoWidth = (CommonParameter.sScreenWidth - CommonUtil.dip2px(mContext, 85)) / 4;
    }

    public MyPostAdapter(Context mContext, List<Message> iniData, int pageSize, int res,
            int loadingRes, int tabIndex, boolean isClickEnable) {
        super(mContext, iniData, pageSize, res, loadingRes);
        this.tabIndex = tabIndex;
        this.isClickEnable = isClickEnable;
        photoWidth = (CommonParameter.sScreenWidth - CommonUtil.dip2px(mContext, 85)) / 4;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void parsePhotos(LinearLayout container, List<Pic> list) {
        container.removeAllViews();

        int length = list.size();
        if (length > 3) {
            length = 3;
        }

        for (int i = 0; i < length; i++) {
            LinearLayout view =
                    (LinearLayout) View.inflate(mContext, R.layout.image_pet_icon, null);
            ImageView icon = (ImageView) view.findViewById(R.id.pet_icon);
            RelativeLayout.LayoutParams params =
                    new RelativeLayout.LayoutParams(photoWidth, photoWidth);
            if (CommonParameter.isLoadImg) {
                ImageLoader.getInstance().displayImage(list.get(i).url, icon);
            } else {
                icon.setImageResource(R.drawable.picture_loading);
            }
            icon.setLayoutParams(params);
            container.addView(view);
        }
    }

    class ViewHolder {
        LinearLayout ll;
        CircleImageView usericon;
        TextView username;
        TextView title;
        ImageView type;
        TextView look;
        TextView care;
        TextView message;
        TextView time;
        ImageView iv_top_right;
        ImageView check_box;
    }

    @Override
    public void nextPage(int start, int size, final ILoadNextPageData<Message> iLoadNextPageData) {
        if (tabIndex == 0) {
            HappyPetApplication.get().getMoccaApi()
                    .getMyPost(UserState.getUserUid(), start, size, new IRequest<List<Message>>() {

                        @Override
                        public void request(List<Message> t) {
                            iLoadNextPageData.loadNextPageData(t);
                        }
                    });
        } else if (tabIndex == 1) {
            HappyPetApplication.get().getMoccaApi()
                    .getMyReply(UserState.getUserUid(), start, size, new IRequest<List<Message>>() {

                        @Override
                        public void request(List<Message> t) {
                            iLoadNextPageData.loadNextPageData(t);
                        }
                    });
        } else if (tabIndex == 2) {
            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .getMyCollect(UserState.getUserUid(), start, size,
                            new IRequest<List<Message>>() {

                                @Override
                                public void request(List<Message> t) {
                                    iLoadNextPageData.loadNextPageData(t);
                                }
                            });
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent, final Message value) {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.ll = (LinearLayout) convertView.findViewById(R.id.ll_pet_icon);
            holder.usericon = (CircleImageView) convertView.findViewById(R.id.user_icon);
            holder.username = (TextView) convertView.findViewById(R.id.user_name);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.type = (ImageView) convertView.findViewById(R.id.iv_top_right);
            holder.look = (TextView) convertView.findViewById(R.id.look_time);
            holder.care = (TextView) convertView.findViewById(R.id.care_time);
            holder.message = (TextView) convertView.findViewById(R.id.message_time);
            holder.check_box = (ImageView) convertView.findViewById(R.id.check_box);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.iv_top_right = (ImageView) convertView.findViewById(R.id.iv_top_right);
            convertView.setTag(holder);
        }
        holder.title.setText(value.subject);
        holder.username.setText(value.author);
        holder.look.setText(value.views);
        holder.care.setText(value.recommend_add);

        SpannableStringBuilder ssb =
                CommonUtil.convetToHtml(CommonUtil.parseEmoji(value.replies), mContext);

        holder.message.setText(ssb);
        holder.time.setText(TimeFormat.format(value.addtime));


        String time = CommonUtil.formatDate();
        String year = time.substring(0, 4);
        String mTime = value.addtime;
        String mYear = mTime.substring(0, 4);
        String mMonth = mTime.split(" ")[0].split("-")[1];
        String mDay = mTime.split(" ")[0].split("-")[2];
        String mHour = mTime.split(" ")[1].split(":")[0];
        String mMin = mTime.split(" ")[1].split(":")[1];

        if (year.equals(mYear)) {// 说明是今年
            if (value.addtime.split(" ")[0].equals(time.split(" ")[0])) {// 说明是今天
                holder.time.setText(mHour + ":" + mMin);
            } else {
                holder.time.setText(mMonth + "-" + mDay);
            }
        } else {
            holder.time.setText(mTime.split(" ")[0]);
        }

        if (CommonParameter.isLoadImg) {
            ImageLoader.getInstance().displayImage(value.userface, holder.usericon);
        } else {
            holder.usericon.setImageResource(R.drawable.person_loading);
        }
        if (Integer.valueOf(value.digest) > 0) {
            if (Integer.valueOf(value.digest) > 0) {
                holder.iv_top_right.setBackgroundResource(R.drawable.jing);
            } else {
                holder.iv_top_right.setBackgroundColor(Color.WHITE);
            }
        }
        parsePhotos(holder.ll, value.pics);

        if (isClickEnable) {
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PostDetailActivity.class);
                    intent.putExtra(Constants.KEY_POST_TID, value.tid);
                    mContext.startActivity(intent);
                }
            });
        }

        return convertView;
    }

}
